<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

class LoginController extends HomeController {

    public function login()
    {
        $this->display();
    }

    /**
     * 登录
     * */
    public function confirm_login123()
    {
        if(IS_POST){

            $input = I('post.');

            $mobile = $input['mobile'];
            $password = $input['password'];

            if(empty($mobile)) api_return(2,'请输入账号');
            if(empty($password)) api_return(2,'请输入密码');

            $map['mobile'] = array('eq',$mobile);
            $map['user_type'] = array(array('eq',3),array('eq',4),'OR');

            $info = M('ucenter_member')->field('id,username,password,iid')->where($map)->find();

            if($info){
                if(think_ucenter_md5($password, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT') == $info['password']){
                    $data['result'] = 1;
                    $data['msg'] = '登录成功';
                    session('user_id', $info['id']);
                    session('user_name', $info['username']);
                    session('iid', $info['iid']);
                }else{
                    $data['result'] = 2;
                    $data['msg'] = '密码错误';
                }
            }else{
                $data['result'] = 2;
                $data['msg'] = '账号不存在';

            }
            echo json_encode($data);exit;
        }
    }

        public function confirm_login()
    {
        if(IS_POST){

            $input = I('post.');

            $mobile = $input['mobile'];
            $password = $input['password'];

            if(empty($mobile)) api_return(2,'请输入账号');
            if(empty($password)) api_return(2,'请输入密码');

            // $map['mobile'] = array('eq',$mobile);
            // $map['user_type'] = array(array('eq',3),array('eq',4),'OR');
            // $map['user_type']=array('like',"%3%");
            $info = M('ucenter_member')->where("FIND_IN_SET(3, user_type) && mobile=$mobile")->field('id,username,password,iid')->find();

            if($info){
                if(think_ucenter_md5($password, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT') == $info['password']){

                    $status=M('institution')->where("id=$info[iid]")->getfield('status');
                    if($status!=1){
                        $data['result'] = 2;
                        $data['msg'] = '暂未审核通过';
                        echo json_encode($data);exit;
                    }

                    $data['result'] = 1;
                    $data['msg'] = '登录成功';
                    session('user_id', $info['id']);
                    session('user_name', $info['username']);
                    session('iid', $info['iid']);
                    $institution_name=M('institution')->where("id=$info[iid]")->getfield('name');
                    session('institution_name',$institution_name);
                    session('Supertube',1);//说明他是超管 就是所有权限
                }else{
                    $data['result'] = 2;
                    $data['msg'] = '密码错误';
                }
            }else{
                // $data['result'] = 2;
                // $data['msg'] = '账号不存在';
                //不是超管 我再判断是不是后台添加的管理员
                $condition['mobile']=$mobile;
                $res=M('account_number')->where($condition)->find();
                if($res){
                    if(think_ucenter_md5($password, '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT') == $res['password']){
                        $data['result'] = 1;
                        $data['msg'] = '登录成功';
                        session('user_id', $res['id']);//没有user_id 但是蒋磊做的时候每个页面都判断了user_id
                        session('iid', $res['iid']);
                        $institution_name=M('institution')->where("id=$res[iid]")->getfield('name');
                        session('institution_name',$institution_name);
                        session('Supertube',2);//说明他不是超管
                    }else{
                        $data['result'] = 2;
                        $data['msg'] = '密码错误';
                    }
                }else{
                    $data['result'] = 2;
                    $data['msg'] = '账号不存在';                   
                }
            }
            echo json_encode($data);exit;
        }
    }
}
